<template>
  <!-- 实现登录--路由导航守卫控制功能--未登录不能访问任何页面 -->
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <div>
        <img src="../assets/heima.png" alt />
        <span>后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse" v-text="isCollapse ? '三' : '|||'"></div>
        <el-menu
          background-color="#333744"
          text-color="#fff"
          active-text-color="#409EFF"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <!-- 一级菜单 动态绑定index,可以防止点其中一项,其他的全部展开,使用item.id,因为只接收字符串,因此+''就可以-->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconObj[item.id]"></i>
              <span>{{ item.authName }}</span>
            </template>
            <!-- 二级菜单 如果不动态绑定index,那么点击一个子菜单其他子菜单也会高亮-->
            <el-menu-item
              :index="'/' + subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="saveNavState('/' + subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{ subItem.authName }}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <!-- 路由占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  components: {},
  data () {
    return {
      menuList: [], // 存放左侧菜单列表
      iconObj: {
        // 字体图标库
        125: 'el-icon-user',
        103: 'el-icon-lock',
        101: 'el-icon-goods',
        102: 'el-icon-s-order',
        145: 'el-icon-s-marketing'
      },
      // 折叠是否 默认否
      isCollapse: false,
      // 被激活的链接地址
      activePath: ''
    }
  },
  created () {
    // 左侧菜单列表,一打开网页就应该加载
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  methods: {
    // 退出登录
    logout () {
      // 清除token
      window.sessionStorage.clear()
      // 清除token之后跳转到 登录页 login
      this.$router.push('/login')
    },
    // 左侧菜单列表
    async getMenuList () {
      const { data: res } = await this.$http.get('/menus')
      if (res.meta.status === 200) {
        this.menuList = res.data
      }
    },
    // 点击折叠与放开
    toggleCollapse () {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState (activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      // 重新赋值
      this.activePath = activePath
    }
  }
}
</script>
<style scoped lang="less">
.home-container {
  height: 100%;
  .el-header {
    background-color: #373d41;
    display: flex;
    justify-content: space-between;
    padding-left: 0;
    align-items: center;
    color: aliceblue;
    font-size: 1.8rem;
    font-weight: 600;
    > div {
      display: flex;
      align-items: center;
      > span {
        margin-left: 1rem;
      }
    }
  }
  .el-aside {
    background-color: #333744;
    .el-menu {
      border: none;
    }
    .toggle-button {
      color: aliceblue;
      font-size: 1rem;
      line-height: 2.4rem;
      text-align: center;
      cursor: pointer;
      letter-spacing: 0.2rem;
    }
  }
  .el-main {
    background-color: #eaedf1;
    font-size: 1.4rem;
  }
}
</style>
